/** 
 * File-Name:APIRepository.java
 *
 * Created on 2011-10-2 下午03:23:18
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package com.debugassistant.dao;

import java.sql.Connection;
import java.util.List;

import neoutil.CommonRepository;
import neoutil.DaoSupport;

import org.apache.log4j.Logger;

import com.debugassistant.dao.dao.APIDao;
import com.debugassistant.entity.APIInfo;
import com.debugassistant.service.exception.DuplicateAPIException;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-10-2 下午03:23:18
 */
public class APIRepository extends CommonRepository {
	private static Logger log = Logger.getLogger(APIRepository.class);
	public static final String SAVE_SUCCESS = "SAVE_SUCCESS";
	public static final String SAVE_ALREADY_EXIST = "SAVE_ALREADY_EXIST";
	public static final String SAVE_INVALID_PARAMETER = "SAVE_INVALID_PARAMETER";
	private static String SAVE_API = "INSERT INTO apiinfo (uuid, methodName, inputParameters, output, comment, throwExceptions, signature, javaDocUrl, entityStatus, entityDatetime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

	public static APIInfo save(APIInfo apiInfo) {
		Connection connection = null;
		try {
			if (apiInfo == null || apiInfo.getMethodName() == null
					|| "".equals(apiInfo.getMethodName())) {
				log.error("The apiInfo parameter is invalid.");
				return null;
			} else {
				connection = DaoSupport.getConnection();
				APIInfo oldApiInfo = isExist(connection, apiInfo
						.getMethodName());
				if (oldApiInfo != null) {
					log.info("API Info Exist! Do NOT Save!");
					return oldApiInfo;
				} else {
					Object[] params = { apiInfo.getUuid(),
							apiInfo.getMethodName().trim(),
							apiInfo.getInputParameters(), apiInfo.getOutput(),
							apiInfo.getComment(), apiInfo.getThrowExceptions(),
							apiInfo.getSignature(), apiInfo.getJavaDocUrl(),
							apiInfo.getEntityStatus(),
							apiInfo.getEntityDatetime() };
					APIDao apiDao = new APIDao(connection);
					apiDao.execute(SAVE_API, params);
					return apiInfo;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}
	}

	public static APIInfo isExist(Connection connection, String methodName)
			throws DuplicateAPIException {
		String sql = "SELECT * FROM apiinfo WHERE methodName=?";
		Object[] params = { methodName };
		APIDao apiDao = new APIDao(connection);
		List<APIInfo> apiInfos = apiDao.find(sql, params);
		if (apiInfos == null || apiInfos.size() == 0) {
			return null;
		} else if (apiInfos.size() == 1) {
			return apiInfos.get(0);
		} else {
			DuplicateAPIException duplicateAPIException = new DuplicateAPIException();
			throw duplicateAPIException;
		}
	}

	public static synchronized APIInfo get(String methodName) {
		APIInfo apiInfo = null;
		Connection connection = null;
		String GET_API = "SELECT * FROM apiinfo WHERE methodName='"
				+ methodName + "'";
		try {
			connection = DaoSupport.getConnection();
			APIDao apiDao = new APIDao(connection);
			List<APIInfo> apiInfos = apiDao.find(GET_API);
			if (apiInfos != null && apiInfos.size() == 1) {
				apiInfo = apiInfos.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection(connection);
		}

		return apiInfo;
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
